<?php
 
	function smarty_block_classes($params, $content, &$smarty, &$repeat) {

		global $db, $config;

		$l = count($smarty->_tag_stack);
		if(empty($params['name'])) $params['name'] = "classes";

		if(empty($smarty->blockvars[$l])) {
			$sqlstr = "select * from {$config->db_prefix}classes where 1=1";
			if (!empty($params['id'])) $sqlstr .= " and id = ".$db->qstr($params['id']);
			if (isset($params['parentid'])) $sqlstr .= " and parentid = ".$db->qstr($params['parentid']);
			if (!empty($params['cond'])) $sqlstr .= " and ({$params['cond']})";
			if (!empty($params['order'])) $sqlstr .= " order by {$params['order']}";
			$limit = empty($params['limit'])?9999:$params['limit'];
			$page = empty($params['page'])?1:$params['page'];
			$pager = new pager($limit, $page);
			$smarty->blockvars[$l] = $pager->execute($db, $sqlstr);

			//
			if(!$smarty->blockvars[$l]) return $repeat = false;
			if(!empty($params['pager'])) $smarty->assign($params['pager'], $pager);
		}
        if (list ($key, $item) = each($smarty->blockvars[$l])) {
            $repeat = true;
            $smarty->assign($params['name'], $item);
            $childrensqlstr = "select * from {$config->db_prefix}classes where parentid={$item['id']} order by {$params['order']}";
            if ($children = $db->GetArray($childrensqlstr)) {
                // var_dump($children);
                $smarty->assign('sub_class', $children);
                foreach ($children as $v) {
                    $subchildrensqlstr = "select * from {$config->db_prefix}classes where parentid={$v['id']} order by {$params['order']}";
                    
                    if ($subchildren = $db->GetArray($subchildrensqlstr)) {
                        $smarty->assign('sub_sub_class', $subchildren);
                    } else {
                        $smarty->assign('sub_sub_class', array());
                    }
                }
            } else {
                $smarty->assign('sub_class', array());
            }
            
            $smarty->_tpl_vars[$params['name']]["layer"] = count(
                    explode(",", $item["path"])) - 3;
        } 
		else {
			$repeat = false;
			reset($smarty->blockvars[$l]);
		}
		
		if(!is_null($content)) print $content;
	
		//
		if(!$repeat) $smarty->blockvars[$l] = array();
	}

?>